<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span class="card-title">门禁配置</span>
      </div>
      
      <el-form 
        ref="configForm" 
        :model="form" 
        :rules="rules" 
        label-width="140px" 
        size="medium"
        v-loading="loading"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="人脸价格" prop="facePrice">
              <el-input-number
                v-model="form.facePrice"
                :precision="2"
                :step="0.01"
                :min="0"
                :max="999999.99"
                placeholder="请输入人脸价格"
                style="width: 100%"
              >
                <template slot="append">元</template>
              </el-input-number>
            </el-form-item>
          </el-col>
          
          <el-col :span="12">
            <el-form-item label="购买一年赠送月份" prop="giftMonthOfByOneYear">
              <el-input-number
                v-model="form.giftMonthOfByOneYear"
                :min="0"
                :max="12"
                placeholder="请输入赠送月份"
                style="width: 100%"
              >
                <template slot="append">个月</template>
              </el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item>
          <el-button 
            type="primary" 
            @click="submitForm"
            :loading="submitLoading"
          >
            保存配置
          </el-button>
          <el-button @click="resetForm">重置</el-button>
          <el-button @click="getConfig" :loading="loading">刷新</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getDoorConfig, updateFacePrice } from '@/api/admin/doorConfig'

export default {
  name: 'DoorConfigPeizhi',
  data() {
    return {
      // 表单数据
      form: {
        facePrice: null,
        giftMonthOfByOneYear: null
      },
      // 表单验证规则
      rules: {
        facePrice: [
          { required: true, message: '人脸价格不能为空', trigger: 'blur' },
          { type: 'number', min: 0, max: 999999.99, message: '价格必须在0-999999.99之间', trigger: 'blur' }
        ],
        giftMonthOfByOneYear: [
          { required: true, message: '赠送月份不能为空', trigger: 'blur' },
          { type: 'number', min: 0, max: 12, message: '赠送月份必须在0-12之间', trigger: 'blur' }
        ]
      },
      // 加载状态
      loading: false,
      submitLoading: false
    }
  },
  created() {
    this.getConfig()
  },
  methods: {
    /** 获取配置信息 */
    getConfig() {
      this.loading = true
      getDoorConfig().then(response => {
        if (response.code === 200) {
          // 如果API只返回成功消息而没有data，使用默认值
          if (response.data && typeof response.data === 'object') {
            this.form = {
              facePrice: response.data.facePrice || 0,
              giftMonthOfByOneYear: response.data.giftMonthOfByOneYear || 0
            }
          } else {
            // API返回成功但没有配置数据，使用默认值
            this.form = {
              facePrice: 0,
              giftMonthOfByOneYear: 0
            }
            this.$modal.msgSuccess('获取配置成功，当前使用默认配置')
          }
        } else {
          this.$modal.msgError(response.msg || '获取配置失败')
        }
      }).catch(() => {
        this.$modal.msgError('获取配置失败')
      }).finally(() => {
        this.loading = false
      })
    },
    
    /** 提交表单 */
    submitForm() {
      this.$refs['configForm'].validate(valid => {
        if (valid) {
          this.submitLoading = true
          updateFacePrice(this.form).then(response => {
            if (response.code === 200) {
              this.$modal.msgSuccess('配置保存成功')
              this.getConfig() // 重新获取最新配置
            } else {
              this.$modal.msgError(response.msg || '配置保存失败')
            }
          }).catch(() => {
            this.$modal.msgError('配置保存失败')
          }).finally(() => {
            this.submitLoading = false
          })
        }
      })
    },
    
    /** 重置表单 */
    resetForm() {
      this.$refs['configForm'].resetFields()
      this.getConfig()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
}

.box-card {
  margin: 0 auto;
  max-width: 800px;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.el-form {
  margin-top: 20px;
}

.el-col {
  padding: 0 10px;
}

.el-input-number {
  width: 100% !important;
}

:deep(.el-input-group__append) {
  background-color: #f5f7fa;
  color: #909399;
  border-left: 0;
}
</style>
